﻿using System.Text;
using RabbitMQ.Client;

namespace RabbitmqPublish
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            IConnectionFactory conFactory = new ConnectionFactory//创建连接工厂对象
            {
                HostName = "113.141.88.219",//IP地址
                Port = 5672,//端口号
                UserName = "guest",//用户账号
                Password = "ym123!@#"//用户密码
            };
            using var con = conFactory.CreateConnection();
            using var channel = con.CreateModel();
            var queueName = "queue";
            //声明一个队列
            channel.QueueDeclare(
                queue: queueName,//消息队列名称
                durable: false,//是否缓存
                exclusive: false,
                autoDelete: false,
                arguments: null
            );
            while (true)
            {
                Console.WriteLine("请输入消息内容:");
                var message = Console.ReadLine();
                //消息内容
                if (message != null)
                {
                    var body = Encoding.UTF8.GetBytes(message);
                    //发送消息
                    channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
                }

                Console.WriteLine("成功发送消息:" + message);
            }
        }
    }
}